<template>
  <div>
    <Row>
      <i-col span="24">
        <Card>
          <Input v-model="form.orderNo" style="width: 200px" placeholder="请输入订单号" clearable @on-enter="searchTable"/>&nbsp;
          <Button @click="searchTable" type="primary">查看订单</Button>&nbsp;
          <Tooltip content="刷新" placement="right">
            <Button icon="md-refresh" type="success" shape="circle" @click="searchTable(false)"></Button>
          </Tooltip>
          <GoodsOrderTableMain
            ref="table"
            :isVerification="true"
            v-on:showDetailModal="showDetailModal"
            v-on:showAttrDetailModal="showAttrDetailModal"
            v-on:showDeliverGoods="showDeliverGoods"
          />
        </Card>
      </i-col>
    </Row>
    <GoodsOrderDetailModal ref="detailModal" />
    <UserDetailDetailModal ref="attrDetailmodal" />

    <!-- 发货详情 -->
    <GoodsOrderDeliveryDetailModal
      ref="goodsOrderDeliveryDetailModal"
      :isVerification="true"
      v-on:searchTable="searchTable"
    />
  </div>
</template>

<script>
import * as utils from '@/api/utils-v2'
import * as ResponseStatus from '@/api/response-status'
import GoodsOrderTableMain from './GoodsOrderTableMain.vue'
import GoodsOrderSearchModal from './GoodsOrderSearchModal.vue'
import GoodsOrderDetailModal from './GoodsOrderDetailModal.vue'
import UserDetailDetailModal from '../user-detail/UserDetailDetailModal.vue'
import GoodsOrderDeliveryDetailModal from './GoodsOrderDeliveryDetailModal.vue'
export default {
  name: 'GoodsOrderMainVerification',
  components: {
    GoodsOrderTableMain,
    GoodsOrderDetailModal,
    UserDetailDetailModal,
    GoodsOrderDeliveryDetailModal,
  },
  data() {
    return {
      urls: {
        oneUserUrl: '/user-detail/admin/one/',
        deliveryUrl: '/goods-order/admin/delivery/'
      },
      modal: {
        orderItemModal: false
      },
      form: {
        orderNo: null
      }
    }
  },
  computed: {},
  mounted() {},
  methods: {
    searchTable() {
      if (!this.form.orderNo) {
        this.$Message.warning('请输入订单号')
        return
      }
      let tableComponent = this.$refs.table
      tableComponent.table.loading = true
      utils.getDataDict(tableComponent.dataDictCodes).then(response => {
        tableComponent.dataDictItems = response
        utils.doGet('/goods-order/admin/order-no/' + this.form.orderNo, {}).then(response => {
          if (response.data.code !== ResponseStatus.OK) {
            tableComponent.$Message.error(response.data.message)
          } else {
            tableComponent.pager.total = response.data.data.total
            tableComponent.table.tableRows = response.data.data.rows
          }
          tableComponent.table.loading = false
        }).catch(error => {
          console.log(error)
          tableComponent.table.loading = false
          tableComponent.$Message.error('加载数据失败，稍候再试')
        })
      }).catch(error => {
        console.log(error)
      })
    },
    showDetailModal(row) {
      let detailModal = this.$refs.detailModal
      detailModal.modal.detail = true
      detailModal.form = row
	  detailModal.getDictItemsDisplayValue()
    },
    showModal(modal) {
      utils.showModal(this, modal)
    },
    cancelModal(modal) {
      utils.cancelModal(this, modal)
    },
    /**
     * 显示详情弹窗
     */
    showAttrDetailModal(id) {
      let url = this.urls.oneUserUrl
      utils
        .doGet(url + id, {})
        .then(res => {
          if (ResponseStatus.OK === res.data.code) {
            const row = res.data.data
            let detailModal = this.$refs.attrDetailmodal
            detailModal.modal.detail = true
            detailModal.form = row
          } else {
            this.$Message.error(res.data.message)
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    /**
     * 发货显示发货详情，包括购买的订单项和物流信息
     */
    showDeliverGoods(row) {
      let detailModal = this.$refs.goodsOrderDeliveryDetailModal
      if (row.orderStatus === 3) {
        // 待发货
        detailModal.canDeliver = true
      }
      utils
        .doGet('/goods-order-logistics/admin/one/' + row.id, {})
        .then(res => {
          if (res.data.code === ResponseStatus.OK) {
            detailModal.modal.detail = true
            detailModal.form = res.data.data
            detailModal.getDictItemsDisplayValue()
          }
        })
        .catch(err => {
          console.log('获取物流信息异常')
        })
        detailModal.orderItemsTable.loading = true
        utils.doPostJson('/goods-order-item/admin/all-cond', {
          orderId: row.id
        }, {}).then(response => {
          if (response.data.code === ResponseStatus.OK) {
            detailModal.orderItemsTable.loading = false
            detailModal.orderItemsTable.tableRows = response.data.data.rows
          }
        }).catch(error => {
          detailModal.orderItemsTable.loading = false
        })
    },
    /**
     * 查看订单项
     */
    showOrderItem(row) {
      let goodsOrderItem = this.$refs.goodsOrderItem
      let searchModal = goodsOrderItem.$refs.searchModal
      searchModal.searchForm.orderId = row.id
      goodsOrderItem.searchTable()
      this.showModal('orderItemModal')
    }
  }
}
</script>

<style>
</style>
